package com.taptech.doufu.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.CartoonScanNewActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.ImageBean;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.util.ScreenUtil;

import java.util.List;

/**
 * Created by longsiyang on 16/6/15.
 */
public class WholeCartoonDetailAdapter extends RecyclerView.Adapter<BaseRecyclerViewViewHolder> {
    private static final int TYPT_PAY = 1;
    private static final int TYPE_IMAGE = 0;
    private CartoonScanNewActivity activity;
    private List<ImageBean> dataList;
    private CartoonScanNewActivity.OnPayPageListen onPayPageListen;
    private CartoonScanNewActivity.OnImageListen onImageListen;


    public WholeCartoonDetailAdapter(CartoonScanNewActivity activity, List<ImageBean> dataList,
                                     CartoonScanNewActivity.OnPayPageListen onPayPageListen,
                                     CartoonScanNewActivity.OnImageListen onImageListen)
    {
        this.activity = activity;
        this.dataList = dataList;
        this.onPayPageListen = onPayPageListen;
        this.onImageListen = onImageListen;
    }

    public void refrushList(List<ImageBean> list) {
        this.dataList = list;
        notifyDataSetChanged();
    }

    @Override
    public BaseRecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType == TYPT_PAY) {
            View view = LayoutInflater.from(activity).inflate(R.layout.cartoon_pay_layout, null);
            return new CartoonPay(activity, view);
        }else{
            View view = LayoutInflater.from(activity).inflate(R.layout.cartoon_img_item_display, null);
            return new CartoonPic(activity, view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (dataList != null && dataList.size() > position) {
            ImageBean bean = dataList.get(position);
            if (bean != null && bean.isNeed_pay()) {
                return TYPT_PAY;
            }
        }
        return TYPE_IMAGE;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewViewHolder holder, int position) {
        holder.setViewHolderChildViewContent(null, position);
    }

    public class CartoonPay extends BaseRecyclerViewViewHolder{

        public TextView tvTitle;
        public TextView btnPay;
        public LinearLayout llAutoPay;
        public ImageView ivAutoPay;
        public LinearLayout payLay;
        public RelativeLayout layFather;
        public RelativeLayout loadingLay;
        public TextView tvTip;
        public CartoonPay(Context context, View itemView) {
            super(context,itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            btnPay = (TextView) itemView.findViewById(R.id.btnPay);
            llAutoPay = (LinearLayout) itemView.findViewById(R.id.llAutoPay);
            payLay = (LinearLayout) itemView.findViewById(R.id.payLay);
            ivAutoPay = (ImageView) itemView.findViewById(R.id.ivAutoPay);
            ivAutoPay.setSelected(true);
            loadingLay = (RelativeLayout) itemView.findViewById(R.id.loadingLay);
            layFather = (RelativeLayout) itemView.findViewById(R.id.layFather);
            tvTip = (TextView) itemView.findViewById(R.id.tvTip);
        }

        @Override
        public void setViewHolderChildViewContent(MineAbstractBean bean, final int position) {
            if (dataList != null) {
                final ImageBean model = dataList.get(position);
                if (onPayPageListen != null) {
                    onPayPageListen.onPageChange(model,position);
                }
                if (activity != null && "1".equals(activity.getAutoPay())) {
                    loadingLay.setVisibility(View.VISIBLE);
                    payLay.setVisibility(View.GONE);
                }else{
                    payLay.setVisibility(View.VISIBLE);
                    loadingLay.setVisibility(View.GONE);
                }
                if (model != null) {
                    if (!TextUtils.isEmpty(model.getTitle())) {
                        tvTitle.setText(model.getTitle());
                    }
                    layFather.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onPayPageListen != null) {
                                onPayPageListen.onClickPage();
                            }
                        }
                    });
                    if (AccountService.getInstance().isLogin()) {

                        StringBuilder sb = new StringBuilder();
                        if (!TextUtils.isEmpty(model.getPrice())) {
                            sb.append("本话价格：");
                            sb.append("<font color='#fe6e6e'>");
                            sb.append(model.getPrice());
                            sb.append("</font>");
                            sb.append(" 豆币");
                            if (!TextUtil.isEmpty(model.getOriginal_price())) {
                                sb.append("（原价").append(model.getOriginal_price()).append("豆币）");
                            }
                        }
                        tvTip.setText(Html.fromHtml(sb.toString()));
                        String btnStr = "确认购买本话";
                        if (activity.readTicketInfo != null && activity.readTicketInfo.getId() > 0) {
                            btnStr = "使用阅读券购买此话";
                        }
                        btnPay.setText(btnStr);
                        btnPay.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (onPayPageListen != null) {
                                    onPayPageListen.onClick(model,ivAutoPay.isSelected(),position);
                                }
                            }
                        });
                        llAutoPay.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ivAutoPay.setSelected(!ivAutoPay.isSelected());
                            }
                        });
                        llAutoPay.setVisibility(View.VISIBLE);
                    } else {
                        llAutoPay.setVisibility(View.INVISIBLE);
                        btnPay.setText("登录");
                        btnPay.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                AccountService.getInstance().jumpToLogin();
                                if (onPayPageListen != null) {
                                    onPayPageListen.onJumpLogin(position);
                                }
                            }
                        });
                    }

                }
            }

        }
    }

    public class CartoonPic extends BaseRecyclerViewViewHolder
    {

        public ImageView imageView;

        public CartoonPic(Context context, View itemView)
        {
            super(context, itemView);
            imageView = (ImageView) itemView.findViewById(R.id.draw_img_item);
        }

        @Override
        public void setViewHolderChildViewContent(MineAbstractBean bean, final int position) {
            if (dataList == null)
                return;
            final ImageBean imageBean = dataList.get(position);
            if (imageBean == null) {
                return;
            }
            if (onImageListen != null) {
                onImageListen.onItemChange(imageBean,position);
            }
            String headUrl = "http://imgold2.doufu.la/";
            String imgUrl = headUrl + (imageBean.getPath());
            try {
                int cropImgWidth = Integer.valueOf(imageBean.getWidth());
                int cropImgHeight = Integer.valueOf(imageBean.getHeight());
                int mWidth = ScreenUtil.getScreenWidth(activity);
                imgUrl = Constant.changeUrlHost(imgUrl);
                Glide.with(WeMediaApplication.applicationContext)
                        .load(imgUrl)
                        .override(mWidth,mWidth * cropImgHeight / cropImgWidth)
                        .placeholder(R.drawable.img_default_loading7)
                        .centerCrop()
                        .into(imageView);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onImageListen != null) {
                            onImageListen.onItemClick(imageBean);
                        }
                    }
                });
            } catch (Exception e) {

            }
        }


    }

    @Override
    public int getItemCount()
    {
        if (dataList != null)
        {
            return dataList.size();
        }
        return 0;
    }
}
